package org.csu.demopetstore.Controller;

import org.csu.demopetstore.Service.AccountService;
import org.csu.demopetstore.Service.CategortService;
import org.csu.demopetstore.entity.Account;
import org.csu.demopetstore.entity.Category;
import org.csu.demopetstore.entity.Item;
import org.csu.demopetstore.entity.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategortService categortService;
    @Autowired
    private AccountService accountService;

    @RequestMapping("/viewCategory")
    public String getCategory(@RequestParam("categoryId")String categoryId, Model model, HttpSession session){
        List<Product> productList = categortService.getProductList(categoryId);
        Category category = categortService.getCategory(categoryId);
        model.addAttribute(productList);
        model.addAttribute(category);
        System.out.println(category);

        Account account = (Account) session.getAttribute("account");
        if (account != null){
            model.addAttribute(account);
        }

        return "catalog/Category";
    }

    @RequestMapping("/viewProduct")
    public String getProduct(@RequestParam("productId")String productId, Model model, HttpSession session){
        Product product = categortService.getProduct(productId);
        List<Item> itemList = categortService.getItemList(productId);
        model.addAttribute(itemList);
        model.addAttribute(product);
        System.out.println(product);

        Account account = (Account) session.getAttribute("account");
        if (account != null){
            model.addAttribute(account);
        }

        return "catalog/Product";
    }

    @RequestMapping("/viewItem")
    public String getItem(@RequestParam("itemId")String itemId, Model model, HttpSession session){
        Item item = categortService.getItem(itemId);
        Product product = categortService.getProduct(item.getProductId());
        model.addAttribute(product);
        model.addAttribute(item);
        System.out.println(item);

        Account account = (Account) session.getAttribute("account");
        if (account != null){
            model.addAttribute(account);
        }

        return "catalog/Item";
    }

    @RequestMapping("/SearchProducts")
    public String searchProducts(@RequestParam("keyword")String keyword, Model model, HttpSession session){
        Account account = (Account) session.getAttribute("account");
        if (account != null){
            model.addAttribute(account);
        }
        List<Product> productList = categortService.searchProduct(keyword);
        model.addAttribute(productList);
        return "catalog/SearchProducts";
    }

    @RequestMapping("/searchProductsAjax")
    @ResponseBody
    public List<Product> searchProductsAjax(@RequestParam("keyword")String keyword){

        List<Product> productList = categortService.searchProduct(keyword);
        return productList;
    }
}
